import sys, util.gzopen, sets, getopt, random

def deserialise(in_file="feature_map.gz"):
	input = util.gzopen.gzopen(in_file,'r')
	num_labels = int(input.readline())
	counter = 0
	labels = {}
	rev_labels = {}
	for line in input:
		tag,id = line.split()
		labels[tag] = int(id)
		rev_labels[int(id)] = tag
		counter+=1
		if counter >= num_labels: break
	return labels,rev_labels

def read_words(in_file):
	input = util.gzopen.gzopen(in_file,'r')
	corpus = []
	curr_sen = {'words':[],'gold':[],'predicted':[]}
	for line in input:
		tokens = line.split()
		if len(tokens) == 0: 
			corpus.append(curr_sen)
			curr_sen = {'words':[],'gold':[],'predicted':[]}
			continue
		curr_sen['words'].append[tokens[0]]
		curr_sen['gold'].append[tokens[1]]
		curr_sen['predicted'].append[tokens[2]]
	if len(curr_sentence['words']) > 0: corpus.append(curr_sen)
	input.close()
	return corpus

class ParseList:
	def __init__(self):
		self.gold = -1
		self.parses = []
	def add(self, parse, is_gold):
		self.parses.append(parse)
		if is_gold: 
			self.gold = len(self.parses)-1
	def num_parses(self):
		return len(parses)
	
def read_parses(in_file):
	input = util.gzopen.gzopen(in_file,'r')
	parses = {}
	for line in input:
		tokens = line.split()
		id = int(tokens[0])
		if id not in parses: parses[id] = ParseList()
		parses[id].add(tokens[2:], int(tokens[1])==1)
	input.close()
	return parses

if __name__ == '__main__':
	predictions_file=None
	parse_file=None
	options, remaining = getopt.getopt(sys.argv[1:], 'p:l:', [])
	for option, value in options:
		if option == '-l': predictions_file = value
		elif option == '-p': parse_file = value
		else:
			print "Usage: python %s -l predictions -p parses"\ % sys.argv[0]
			print
			print "  -l predicted labels"
			print "  -p parses" 
			sys.exit(1)
	assert predictions_file and parse_file

	# load data
#labels,rev_labels = deserialise(features_file)
	parses = read_parses(parse_file)
	corpus, rev_corpus = read_words(predictions_file)
	
	print predictions_file[0]
	print parse_file[0]
